from django.shortcuts import render,HttpResponse,redirect
from .models import Doctor,Recipe
from django.db.models import Q
from django.contrib.auth.models import User
from django.core.paginator import Paginator
from django.contrib.auth import login,logout,authenticate
# Create your views here.
def index(request):
    return HttpResponse('这是首页')


def register(request):
    if request.method == 'GET':
        return render(request, 'register.html')
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        User.objects.create_user(username=username,password=password)

        return redirect('text:loginx')


def loginx(request):
    if request.method == 'GET':
        message = ''
        return render(request,'loginx.html',{'message':message})
    if request.method == 'POST':
        uname = request.POST.get('uname')
        pwd = request.POST.get('pwd')
        Doctor.objects.create(uname=uname,pwd=pwd)
        if pwd == pwd:
            return HttpResponse('登录成功')
        else:
            return render(request,'loginx.html',{'error':'密码不正确'})


def recipe_add(request):
    if request.method == 'GET':
        recipe = Doctor.objects.all()
        return render(request,'recipe_add.html',{'recipe':recipe})
    if request.method == 'POST':
        cname = request.POST.get('cname')
        result = request.POST.get('result')
        doctor = request.POST.get('doctor')
        hname = request.POST.get('hname')
        Recipe.objects.create(
            cname=cname,
            result=result,
            doctor=doctor,
            hname=hname,
            doctor_id=doctor

        )
        return redirect('text:recipes')
def recipes(request):
    id = request.GET.get('id')
    if id:
        rec = Recipe.objects.filter(Q(cname__icontains=id)).all()
    else:
        rec = Recipe.objects.all()

    object_list, per_page = rec, 3
    pagex = Paginator(object_list, per_page)
    max_page = pagex.num_pages
    page = request.GET.get('page', 1)
    try:
        page = int(page)
    except:
        page = 1

    if page > max_page:
        page = max_page
    if page < 1:
        page = 1

    res = pagex.get_page(page)
    rec = res.object_list
    return render(request,'recipes.html',{'rec':rec,'id':id,'page':page})



def recipe(request):
    pass

def recipe_update(request,bid):
    recipe = Recipe.objects.get(id=bid)
    doctor = Doctor.objects.all()
    if request.method == 'GET':
        return render(request,'recipe_update.html',{'recipe':recipe,'doctor':doctor})
    if request.method == 'POST':
        cname = request.POST.get('cname')
        result = request.POST.get('result')
        doctor = request.POST.get('doctor')
        hname = request.POST.get('hname')

        recipe.cname = cname
        recipe.result = result
        recipe.doctor = doctor
        recipe.hname = hname
        recipe.save()
        return redirect('text:recipes')

def recipe_del(request):
    bid = request.GET.get('id')
    recipe = Recipe.objects.get(id=bid)
    recipe.delete()
    return  redirect('text:recipes')